//
//  EPAttachView.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-1-23.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EPAttachView.h"

@interface EPAttachView () <EPAttachItemDelegate> {
    CGFloat itemWidth;
    CGFloat itemPadding;
}

@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) NSMutableArray *contentArray;

@end

@implementation EPAttachView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _contentView.contentSize = frame.size;
        _contentView.bounces = NO;
        _contentView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_contentView];
        [self setDefaultValues];
        [self reloadItemData];
    }
    return self;
}

- (void)setDefaultValues {
    
    self.contentArray = [[NSMutableArray alloc] initWithCapacity:10];
    itemWidth = CurrentDeviceISiPad ? 160 : 80;
    itemPadding = CurrentDeviceISiPad ? 20 : 10;
}

- (void)reloadItemData {
    dispatch_async(dispatch_get_main_queue(), ^{

        NSInteger numberOfItem = 0;
        
        if (_delegate && [_delegate respondsToSelector:@selector(numberOfItemInepattachView:)]) {
            numberOfItem = [_delegate numberOfItemInepattachView:self];
        }
        
        for (EPAttachItem *item in _contentView.subviews) {
            [item removeFromSuperview];
        }
        [_contentArray removeAllObjects];
        
        if (numberOfItem == 0) {
            [_contentView setNeedsDisplay];
            [self setNeedsDisplay];
            return;
        }
        
        [self getCustomValue];
        
        
        CGFloat frameHeight = self.bounds.size.height;
        CGFloat startX = itemPadding;
        for (int i=0; i<numberOfItem; i++) {
            EPAttachItem *item = [_delegate epattachView:self itemAtIndex:i];
            //item.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:[[NSString stringWithFormat:@"0.%d5", i] floatValue]];
            CGFloat itemHeight = item.frame.size.height;
            CGFloat top = (frameHeight - itemHeight)*0.5;

            CGRect itemFrame = item.frame;
            itemFrame.origin = CGPointMake(startX, top);
            item.frame = itemFrame;
            item.delegate = self;
            startX += (item.frame.size.width + itemPadding);
            [item setNeedsDisplay];

            [_contentView addSubview:item];
            [_contentArray addObject:item];
        }

        _contentView.contentSize = CGSizeMake(startX, frameHeight);
        [_contentView setNeedsDisplay];
        [self setNeedsDisplay];
    });
}

- (void)getCustomValue {

    if (_delegate && [_delegate respondsToSelector:@selector(widthOfItemInepattachView:)]) {
        itemWidth = [_delegate widthOfItemInepattachView:self];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(paddingOfItemInepattachView:)]) {
        itemPadding = [_delegate paddingOfItemInepattachView:self];
    }
}

#pragma mark - EPAttachItemDelegate

- (void)itemSelected:(EPAttachItem *)item {
    if (_delegate && [_delegate respondsToSelector:@selector(epattachView:didSelected:)]) {
        NSUInteger index = [_contentArray indexOfObject:item];
        if (index != NSNotFound) {
            [_delegate epattachView:self didSelected:index];
        }
    }
}

@end
